<script setup>
import { ref } from 'vue'
import {updatePasswordService} from '@/api/user/user.js'
import {ElMessage} from "element-plus";


//重置密码模型
const resetPassword = ref({
  old_pwd:'',
  new_pwd:'',
  re_pwd:''
})
//检查确认密码是否正确
const checkRePassword=(rule,value,callback)=>{
  if(value===''){
    callback(new Error('确认密码不能为空'))
  }else if(value!==resetPassword.value.new_pwd){
    callback(new Error('确认密码错误'))
  }else {
    callback()
  }
}
//校验规则
const rules = {
  old_pwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    {
      pattern: /^\S{5,16}$/,
      message: '密码必须是5-16位的非空字符串',
      trigger: 'blur'
    }
  ],
  re_pwd: [ { validator: checkRePassword, trigger: 'blur' } ],
}


//修改用户密码
const updatePassword= async ()=>{
  await updatePasswordService(resetPassword.value)
  ElMessage.success("修改密码成功")
}
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>重置密码</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form :model="resetPassword" :rules="rules" label-width="100px" size="large">
          <el-form-item label="原密码" prop="old_pwd">
            <el-input type="password" v-model="resetPassword.old_pwd" placeholder="请输入原密码"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input type="password" v-model="resetPassword.new_pwd" placeholder="请输入新密码"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="re_pwd">
            <el-input type="password" v-model="resetPassword.re_pwd" placeholder="请输入确认密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updatePassword">修改密码</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>